package ui.handler;

import org.eclipse.core.commands.*;
import org.eclipse.ui.*;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.IShowInSource;
import org.eclipse.ui.views.properties.PropertySheet;
import org.eclipse.ui.views.properties.PropertyShowInContext;


// Referenced classes of package org.eclipse.ui.views.properties:
//            PropertySheet, PropertyShowInContext

public class NewPropertyViewHandler extends AbstractHandler {
	private String viewId;
	public static final String ID = "com.eugene.ui.commands.NewPropertyView";
	private static final boolean PIN_NEW_PROPERTY_VIEW;

	static {
		PIN_NEW_PROPERTY_VIEW = Boolean.valueOf(
				System.getProperty("org.eclipse.ui.views.properties.pinNewPV",
						Boolean.FALSE.toString())).booleanValue();
	}
	
	public NewPropertyViewHandler() {
	}

	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchPart activePart = HandlerUtil.getActivePartChecked(event);
		PropertyShowInContext context = getShowInContext(event);
		try {
			PropertySheet sheet = findPropertySheet(event, context);
			sheet.show(context);
			if (activePart instanceof PropertySheet) {
				PropertySheet parent = (PropertySheet) activePart;
//	Set Pinned to Old Property
				parent.setPinned(true);
			} else if (!sheet.isPinned())
				sheet.setPinned(PIN_NEW_PROPERTY_VIEW);
		} catch (PartInitException e) {
			throw new ExecutionException("Part could not be initialized", e);
		}
		return null;
	}

	protected PropertyShowInContext getShowInContext(ExecutionEvent event)
			throws ExecutionException {
		IWorkbenchPart activePart = HandlerUtil.getActivePartChecked(event);
		if (activePart instanceof PropertySheet) {
			PropertySheet sheet = (PropertySheet) activePart;
			return (PropertyShowInContext) sheet.getShowInContext();
		}
		IShowInSource adapter = (IShowInSource) activePart.getAdapter(org.eclipse.ui.part.IShowInSource.class);
		if (adapter != null) {
			org.eclipse.ui.part.ShowInContext showInContext = adapter.getShowInContext();
			return new PropertyShowInContext(activePart, showInContext);
		} else {
			return new PropertyShowInContext(activePart, HandlerUtil.getShowInSelection(event));
		}
	}

	protected PropertySheet findPropertySheet(ExecutionEvent event,	PropertyShowInContext context) 
														throws PartInitException,ExecutionException {
		IWorkbenchPage page = HandlerUtil.getActivePartChecked(event).getSite()
				.getPage();
		String secondaryId = null;
		viewId =HandlerUtil.getActivePartId(event);
		secondaryId = Long.toString(System.currentTimeMillis());
		return (PropertySheet) page.showView(viewId, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
//		return (PropertySheet) page.showView(viewId, secondaryId, IWorkbenchPage.VIEW_VISIBLE);
		
//		
//		
////		if (HandlerUtil.getActivePart(event) instanceof PropertySheet) {
//		if (HandlerUtil.getActivePart(event) instanceof MarketVariableView) {	
//			viewId =HandlerUtil.getActivePartId(event);
//			System.out.println("uztest ZZZ"+ viewId);
//			secondaryId = Long.toString(System.currentTimeMillis());
//		} else {
//			IViewReference refs[] = page.getViewReferences();
//			System.out.println(secondaryId + "uztest xxx"+ viewId + refs.length);
//			for (int i = 0; i < refs.length; i++) {
//				IViewReference viewReference = refs[i];
//				if (!"org.eclipse.ui.views.PropertySheet".equals(viewReference.getId())){
//					System.out.println("For In"+ viewReference.getId());
//					continue;
//				}
//				viewId = viewReference.getId();
//				System.out.println("uztest yyy"+ viewId);
//				secondaryId = Long.toString(System.currentTimeMillis());
//				PropertySheet sheet = (PropertySheet) viewReference.getView(true);
////				System.out.println("uztest YYY"+ sheet.getTitle());
////				if (sheet.isPinned()
////						&& (!sheet.isPinned() || !sheet.getShowInContext().equals(context)))
////					continue;
////				secondaryId = sheet.getViewSite().getSecondaryId();
////				break;
//			}
//
//		}
//		return (PropertySheet) page.showView(viewId, secondaryId, 1);
////		return (PropertySheet) page.showView(
////				"com.eugene.ui.views.MarketVariablePropertyView", secondaryId, 1);
////		return (PropertySheet) page.showView(
////				"org.eclipse.ui.views.PropertySheet", secondaryId, 1);
	}

}


/*
	DECOMPILATION REPORT

	Decompiled from: C:\Users\kidon\workspace\org.eclipse.ui.views\org.eclipse.ui.views_3.4.1.M20090826-0800.jar
	Total time: 19 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/
